﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PTUDW_CNTN2010_F_CDT.Models;
using PTUDW_CNTN2010_F_CDT.Filters;

namespace PTUDW_CNTN2010_F_CDT.Controllers
{
    [LanguageFilter]
    public class CommentController : Controller
    {
        EcommerceDBContext db = new EcommerceDBContext();

        [HttpPost]
        public ActionResult AddComment(ProductDetailsModel model)
        {
            string s = Request.Form["id"];
            string returnUrl = Request.Form["returnurl"];
            if (ModelState.IsValid)
            {
                Comment comment = new Comment
                {
                    Name = model.CommentModel.Name,
                    ProductId = int.Parse(s),
                    Email = model.CommentModel.Email,
                    Comments = model.CommentModel.Comment,
                    CommentDate = DateTime.Now,
                };

                db.Comments.Add(comment);
                db.SaveChanges();
            }

            return Redirect(returnUrl);
        }

        [HttpGet]
        public ActionResult ViewComments()
        {
            return View();
        }

        [HttpPost]
        [ChildActionOnly]
        public ActionResult ViewComments(int ProductId)
        {
            var comments = db.Comments.Where(c => c.ProductId == ProductId).ToList();

            return PartialView(comments);
        }
    }
}
